<?php
	defined( "AZ_EXE_" ) or die ( "CORE VARIABLE IS NOT DEFINED." );
	class CategoryController extends Controller {
		public function index(){
			$start = Basic::getVar("start");
			$perpage = Basic::getVar("perpage");
			$search = Basic::getVar("search");
			$start = is_numeric($start)&&$start>-1?$start:0;
			$perpage = is_numeric($perpage)&&$perpage>-1?$perpage:15;
			$searchColumns = Basic::getVar("search_column")?Basic::getVar("search_column"):false;
			$sortColumn = Basic::getVar("sort_column")?Basic::getVar("sort_column"):"id,title_en";
			$sortCriteria = Basic::getVar("sort_criteria")?Basic::getVar("sort_criteria"):false;
			
			$category = $this->loadModel( "category" );
			
			$categoryRecords = $category->display($start,$perpage,$search,$searchColumns,$sortColumn,$sortCriteria) ;
			$totalRecords = $category->getTotalRecords();
			if ( $start < $totalRecords ){
				$this->set("totalRows",$totalRecords);
			}elseif ( $start >= $totalRecords ) {
				$start = $start-20;
				$categoryRecords = $category->display($start,$perpage,$search,$searchColumns,$sortColumn,$sortCriteria) ;
				$this->set("totalRows",$totalRecords);
			}
			$this->set("currentPage",$start);
			$this->set("perpage",$perpage);
			$this->set("search",$search);
			$this->set("searchColumns",$searchColumns);
			$this->set("sortColumn",$sortColumn);
			$this->set("sortCriteria",$sortCriteria);
			$this->set("search",$search);
			$this->set("categorys",$categoryRecords);
			$this->set("title","SMECA CLINIC - Resource List");
			$this->set("selectedMenu","CAT");
			$this->renderDefault("display","category");
			
		}
		public function view($id = 0){
			$this->set( "title" , "Categories > View - SMECA" );
			$categories = $this->loadModel( "categories" );
			$this->set( "records" , $categories->view($id) );
			$this->renderDefault("view", "categories");
		}
		public function edit($id = 0){
			$this->set( "title" , "Categories > Edit - SMECA" );
			$id = Basic::getVar("id");
			$category = $this->loadModel("Category");
			if ( $id ){
				$categoryinfo = $category->find( $id );
				$this->set("category",$categoryinfo[0]);
				$this->set("parents", $category->getParent());
				$this->set('menutypes', $category->getMenuType());
				$this->set("menus", $category->getMenu());
				$this->set("selectedMenu","CAT");
				$this->renderDefault("edit", "category" );
			}else{
				$this->redirect( array("com"=>"category","action"=>"index"));
			}
		}
		public function editprocess(){
			$menu = $this->loadModel("Category");
			if(!empty($_POST)){
				$this->set("info","Controller information is updated succeffully.");
				$menu->update($_POST['data']);
			}else{
				$this->set("info","Failed to update menu information.");
			}
			$this->redirect( array("com"=>"category","action"=>"index"));
		}
		public function add(){
			$category = $this->loadModel( "category" );
			if(empty($_POST)){//print_r($category->getMenu());die();
				$this->set( "title" , "Categories > Add - SMECA" );
				$this->set("parents", $category->getParent());
				$this->set('menutypes', $category->getMenuType());
				$this->set("menus", $category->getMenu());
				$this->set("menus2", $category->getMenu2());
				$this->set("selectedMenu","CAT");
				$this->renderDefault("add", "category");
			}
		}
		public function addprocess(){
			$category = $this->loadModel( "category" );
			$category->add($_POST['data']);
			$this->redirect( array("com"=>"category","action"=>"index"));
		}
		public function delete($id = ""){
			$categories = $this->loadModel( "category" );
			if(!empty($id)){
				$categories -> delete($id);
				$this->redirect( array("com"=>"category","action"=>"index"));
			}else{
				$this->redirect( array("com"=>"category","action"=>"index"));
				// redirect
			}
		}
		public function getCategory(){
			$menu_id = Basic::getVar('menu_id');
			$category = $this->loadModel( "category" );
			$categories = $category->getCategory($menu_id);
			$options = "<option value='' >-- Please Select Parent --</option>";
			if(isset($categories)){
				foreach($categories as $cat){
					$options .= '<option value="' . $cat->id . '" >' . $cat->title . '</option>';
				}
			}
			echo $options;
		}
        public function articlespage(){
            Import::AppLib("word");
            
            $cate_id = Basic::getVar('catid');
            $page = Basic::getVar("page");
            $page = $page==false?1:$page;
            $art = $this->loadModel( "category" );
            
            $categories = $art->getCategoryByCateID($cate_id,$page,1);
            $this->set('categories', @$categories[0]);//die(Basic::pr($categories));
            $articles = $art->getArticle($cate_id,1,$page);
            
            if(count($articles) <= 0){
                $arr = new stdClass();
                $arr->cate_id = $categories[0]->cate_id;
                $arr->cate_title = $categories[0]->cate_title;
                $arr->cate_description = $categories[0] -> cate_description;                
                $articles[0] = $arr;
            }
            
            $this->set('articles',$articles);//die(Basic::pr($articles));
            $this->set( "title" , "SMECA CLINIC - List Article " );
            $this->renderDefault("articles","category");
        }
		public function articles(){
			if($_GET['mid'] == "22"){
				$this->redirect(array("com"=>"resource","action"=>"store", "mid" => $_GET['mid'], "catid" => $_GET['catid']));
			}
			Import::AppLib("word");
			
			$track = $this->loadModel("TrackVisitor");
			$track -> trackVisitor();
			$visitors = $track->getVisitor();
			$this->set('visitors',$visitors);
			
			$cid = Basic::getVar('catid');
            $this->set("cate_id",$cid);
            $mid = Basic::getVar('mid');
            $this->set("menu_id",$mid);
            $page = Basic::getVar("page");
            $page = $page==false?1:$page;
            $page = isset($_SESSION['nav']['page-article-by-menu'])?$_SESSION['nav']['page-article-by-menu']:$page;
            $perpage = 10 ;
            $this->set("page",$page);
			$art = $this->loadModel( "category" );
			$banner = $this->loadModel( "Banner" );
			
			$banner = $this->loadModel( "Banner" );
			$this->set("banners",$banner->display());
			$menu = $this->loadModel("Menu");
			$menus = $menu->getMenu("Section Menues","en");
			$footer_menus = $menu->getMenu("Section Footer","en");
            
			$this->set('menus',$menus);
			$this->set('footerMenues',$footer_menus);
			$this->set("rightBanners",$banner->rightBanners());
			$this->set("topBanners",$banner->topBanners());
			
			$categories = $art->getCategoryByCateID($cid);
			$this->set('categories', @$categories[0]);//die(Basic::pr($categories));
			$this->set("perpage",$perpage);
			$this->set("page", $page);
			$articles = $art->getArticle($cid,$perpage,$page);
            
			if(count($articles["articles"]) <= 0){
				$arr = new stdClass();
				$arr->cate_id = $categories[0]->cate_id;
				$arr->cate_title = $categories[0]->cate_title;
				$arr->cate_description = $categories[0] -> cate_description;				
				$articles['articles'] = array($arr);
			}
            
			$this->set('articles',$articles);//die(Basic::pr($articles));
			$this->set( "title" , "SMECA CLINIC - " . @$categories[0] -> cate_title );
			$this->renderLayout("frontListArticle");
            
		}
        public function catedetails(){
            Import::AppLib("word");
            $cate_id = Basic::getVar('catid');
            $menu_id = Basic::getVar('mid');
            $page = Basic::getVar("page");
            $page = $page==false?1:$page;
            $_SESSION['nav']['menuid-article-by-menu'] = $menu_id;
            $_SESSION['nav']['page-article-by-menu'] = $page;
            $perpage = 10 ;
            $this->set("page",$page);
            $art = $this->loadModel( "category" );

            $this->set("perpage",$perpage);
            $articles = $art->getArticle($cate_id,$perpage,$page);
            
            $this->set('articles',$articles);//die(Basic::pr($articles));
            $this->renderDefault("catedetails","category");
        }
		public function getarticle(){
			Import::AppLib("word");
            $menu= Basic::getVar("menu");
            $menu_id = Basic::getVar("mid");
            $cate_id = Basic::getVar("cid");
            $page = !Basic::getVar("page")?1:Basic::getVar("page");
            $_SESSION['nav']['menuid-article-by-menu'] = $menu_id;
            $_SESSION['nav']['page-article-by-menu'] = $page;
            $this->set("menu_id",$menu_id);
            $this->set("menu",$menu);
            $this->set("cate_id",$cate_id);
			$art = $this->loadModel( "category" );//die($menu);
			if($menu == 0) {
			    $articles = $art->getArticle($cate_id,5,$page);
            }
			else {
			    $articles = $art->getArticleByMenu($menu_id,5,$page);
            }
            $this->set("articles",$articles);
            $this->renderDefault("categorybymenu","category");
		}
	}
?>